package cn.fl.bizflow.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.List;

import org.apache.bcel.generic.DADD;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cmm.comm.facade.upload.constant.SysUlFilesRecConstant;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.bizflow.vo.RskUsrAchLogVO;
import cn.fl.capital.vo.CapFundVO;
import cn.lb.framework.poi.excel.ExcelImportUtil;
import cn.lb.framework.poi.excel.entity.ImportParams;

/**
 * 绩效数据导入
 * @author zhangtong
 *
 */
public class ImportMyAchienementsList {

	private static final Logger logger = LoggerFactory.getLogger(ImportMyAchienementsList.class);

    /**
     * 导入绩效
     * @param 
     * @return
     */
    public static List<RskUsrAchLogVO> importXls(String filePath){
        logger.info("导入绩效="+filePath);
        ImportParams params = new ImportParams();
        params.setTitleRows(0);//标题占用行数
        params.setHeadRows(1);//表头占用行数
        params.setNeedSave(false);//是否保存文件

        List<RskUsrAchLogVO> list = null;
        if(!filePath.contains("http")){
            File file=new File(PropertiesCacheUtils.getProperty(SysUlFilesRecConstant.SYS_FILE_UPLOAD_ROOT_PATH)+filePath);
            if(file.exists()){
                list = ExcelImportUtil.importExcel(file, RskUsrAchLogVO.class, params);
            }
        }else{
            InputStream in= null;
            URLConnection connection=null;
            try {
                URL url=new URL(filePath);
                connection=url.openConnection();//打开连接 获得URLConnection类的对象
                connection.setConnectTimeout(10000);
                connection.setReadTimeout(60000);
                in=connection.getInputStream();
                list = ExcelImportUtil.importExcel(in, RskUsrAchLogVO.class, params);
            } catch (IOException e) {
                BaseLogUtils.error(logger,"importXls","导入绩效读取上传文件出错，"+e.getMessage());
			BaseLogUtils.error(logger, "-", "异常", e);
            } catch (Exception e) {
                BaseLogUtils.error(logger,"importXls","导入绩效读取上传文件出错,"+e.getMessage());
			BaseLogUtils.error(logger, "-", "异常", e);
            } finally {
                if(null!=in){
                    try {
                        in.close();
                    } catch (IOException e) {
			BaseLogUtils.error(logger, "-", "异常", e);
                    }
                }
            }
        }

        if(CollectionUtils.isNotEmpty(list)){
            BaseLogUtils.info(logger,"importXls","导入绩效="+filePath+",记录条数="+list.size());
            StringBuffer errMsg=null;
            for(RskUsrAchLogVO vo:list){
                vo.setAstart("0");//正常
                errMsg=new StringBuffer("");
                if(vo.getAchMth()==null||"".equals(vo.getAchMth())){
                    errMsg.append("提成所属月份为空;");
                    vo.setAchMth(new Date());
                }
                if(vo.getAchPayMth()==null||"".equals(vo.getAchPayMth())){
                    errMsg.append("提成发送月份为空;");
                    vo.setAchPayMth(new Date());
                }
                if("1".equals(vo.getAstart())){
                    vo.setEstart(errMsg.toString());
                }
            }
        }else{
            BaseLogUtils.info(logger,"importXls","导入绩效="+filePath+",记录条数=0");
        }
        return list;
    }






}
